/** @file         client.c
 *  @brief        简要说明
 *  @details      详细说明
 *  @author       lzm
 *  @date         2021-06-10 09:49:46
 *  @version      v1.0
 *  @copyright    Copyright By lizhuming, All Rights Reserved
 *  @blog         https://www.cnblogs.com/lizhuming/
 **********************************************************
 *  @LOG 修改日志:
 **********************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <pthread.h>

#define TCPC_IPV4 (0)
#define TCPC_IPV6 (1)

#if TCPC_IPV4
#define sHOST "127.0.0.1" // 服务器端IP
#elif TCPC_IPV6
#define sHOST "2000::12" // 服务器端IP
#endif
#define sPORT 9998                  // 服务器进程端口号

#define BUFFER_SIZE (1*1024)   // 数据域大小

// 注意: 接收缓冲区对应对方的发送缓冲区小于等于就不会出现对方接收多次。


/**
  * @brief  读 线程
  * @param 
  * @retval 
  * @author
  */
void *client_read_fun(void *arg) 
{
    int recv_len = 0; // 接收长度
    char recv_buff[BUFFER_SIZE] = "";
    int sockfd = *(int *)arg; // 已连接的 socket

    while((recv_len = recv(sockfd, recv_buff, sizeof(recv_buff), 0)) > 0)
    {
        printf("[%d]recv_buff:%s\r\n", sockfd, recv_buff);
    }

    printf("exit read thread!");
    return NULL;
}

/**
  * @brief  主函数
  * @param 
  * @retval 
  * @author
  */
int main(void)
{
    int sockfd; // 套接字描述符
    int ret;    // 返回结果
    #if TCPC_IPV4
    struct sockaddr_in server; // 套接字地址
    #elif TCPC_IPV6
    struct sockaddr_in6 server; // 套接字地址
    struct sockaddr_in6 client_addr; // 套接字地址
    #endif
    char data_buffer[BUFFER_SIZE];  // app 数据域
    pthread_t thread_read_id; // 读 线程 id


    struct addrinfo hints;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family   = AF_INET6;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags    = AI_ADDRCONFIG;
    // ret = getaddrinfo("[2000::11]", NULL, &hints, &server);
    ret = getaddrinfo("2000::11", NULL, &hints, &server);
    if(ret < 0) { printf("getaddrinfo failed!\n"); exit(1); }
    printf("getaddrinfo success!\n");


    /* [1] 创建套接字 */
    #if TCPC_IPV4
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    #elif TCPC_IPV6
    sockfd = socket(AF_INET6, SOCK_STREAM, 0);
    #endif
    if(sockfd == -1)
    {
        printf("creat socket failed!\n");
        exit(1);
    }

    /* [2] 初始化地址 */
    memset(&server, 0x00, sizeof(server));
    #if TCPC_IPV4
    server.sin_family = AF_INET;
    server.sin_port = htons(sPORT);
    server.sin_addr.s_addr = inet_addr(sHOST);
    #elif TCPC_IPV6
    memset(&client_addr, 0x00, sizeof(client_addr));
    client_addr.sin6_family = AF_INET6;
    client_addr.sin6_port = htons(0);
    // inet_pton(AF_INET6, "fe80::39b2:f7f6:b4a8:fb8d", &client_addr.sin6_addr);
    client_addr.sin6_addr = in6addr_any;
    ret = bind(sockfd, (struct sockaddr *)&client_addr, sizeof(client_addr));
    if(ret < 0)
    {
        printf("bind client failed![%d]\n", errno);
        close(sockfd);
        exit(1);
    }

    server.sin6_family = AF_INET6;
    server.sin6_port = htons(sPORT);
    ret = inet_pton(AF_INET6, sHOST, &server.sin6_addr);
    if(ret <= 0)
    {
        printf("inet_pton failed![%d]\n", errno);
        close(sockfd);
        exit(1);
    }
    #endif

    /* [3] 建立TCP连接 */
    ret = connect(sockfd, (struct sockaddr *)&server, sizeof(server));
    if(ret < 0)
    {
        printf("connect server failed![%d]\n", errno);
        close(sockfd);
        exit(1);
    }

    printf("connect server success!");

    /* [4][1] 创建 读 线程 */
    //pthread_create(&thread_read_id, NULL, (void *)client_read_fun, (void *)&(sockfd));
    //pthread_detach(thread_read_id); // 线程分离

    /* [4][2] 进入循环 写 */
    while(1)
    {
        printf("please enter some text:");
        fgets(data_buffer, BUFFER_SIZE, stdin);

        // 输入end，退出循环
        if(strncmp(data_buffer, "end", 3) == 0)
            break;

        send(sockfd, data_buffer, sizeof(data_buffer), 0);

        bzero(data_buffer, sizeof(data_buffer));

        recv(sockfd, data_buffer, sizeof(data_buffer), 0);

        printf("data_buff:%s\r\n", data_buffer);

    }

    /* [5] 退出 */
    close(sockfd);
    exit(0);
}